import Cell from './cell';

class TableData {
    constructor(){
        this.dataset = [];
    }

    setData(dataset){
        this.dataset = dataset || [];
    }

    getRange(start = {}, end = {}, fixed = {}){
        let rowStart = start.row;
        let rowEnd = end.row;
        let colStart = start.col;
        let colEnd = end.col;

        let range = [];
        // 列号的行
        let indexRow = [];
        indexRow.push(new Cell('c', 'r', 'cr'));
        for(let i = colStart; i <= colEnd; i++){
            indexRow.push(new Cell('c', i, i));
        }
        range.push(indexRow);

        // 上部 - 固定行
        let fixedRowsTop = fixed.top;
        for(let i = 0; i <= fixedRowsTop; i++){

        }

        // 行
        for(let i = rowStart; i <= rowEnd; i++){
            let row = [];
            // 行号
            row.push(new Cell(i, 'r', {
                text: i,
                style: 'width:40px',
                classList: ['rownum']
            }));

            let dataRow = this.dataset[i] || [];
            for(let j = colStart; j <= colEnd; j++){
                let cell = dataRow[j];
                if(!(cell instanceof Cell)){
                    cell = new Cell(i, j, dataRow[j]);
                    dataRow[j] = cell;
                }
                row.push(cell);
            }
            range.push(row);
        }

        return range;
    }
    _getRange(start = {}, end = {}){
        let rowStart = start.row;
        let rowEnd = end.row;
        let colStart = start.col;
        let colEnd = end.col;

        let range = [];

        for(let i = rowStart; i <= rowEnd; i++){
            let row = [];

            let dataRow = this.dataset[i] || [];
            for(let j = colStart; j <= colEnd; j++){
                let cell = dataRow[j];
                if(!(cell instanceof Cell)){
                    cell = new Cell(i, j, dataRow[j]);
                    dataRow[j] = cell;
                }
                row.push(cell);
            }
            range.push(row);
        }

        return range;
    }

    getRow(rowIndex, colStart, colEnd){
        return this.getRange({
            row: rowIndex,
            col: colStart
        }, {
            row: rowIndex,
            col: colEnd
        });
    }
    
}

export default TableData;